#include "cpp.h"
#include <QFontDatabase>
#include <QFileInfo>
#include <QMessageBox>

CPlusPLus::CPlusPLus()
{
    m_cmpPrc = new QProcess();
}

CPlusPLus::~CPlusPLus()
{
}


const QString CPlusPLus::name()
{
    return QString("C++ ver. 0.1a");
}

const QString CPlusPLus::author()
{
    return QString("Edward Sarkisyan [edward.sarkisyan@gmail.com]");
}

void CPlusPLus::configureEditor(QsciScintilla* editor)
{
    setupDefaultLexer(editor);

    //setup EOF mode
    editor->markerDefine(QsciScintilla::RightTriangle,1);
    editor->markerDefine(QsciScintilla::Circle,2);
    #if defined Q_WS_X11
        editor->setEolMode(QsciScintilla::EolUnix);
    #elif defined Q_WS_WIN
        editor->setEolMode(QsciScintilla::EolWindows);
    #elif defined Q_WS_MAC
        editor->setEolMode(QsciScintilla::EolMac);
    #endif
    // setup folding
    editor->setFolding(QsciScintilla::BoxedTreeFoldStyle);

    //setup autopindentation
    editor->setAutoIndent(true);
    editor->setIndentationGuides(false);
    editor->setIndentationsUseTabs(true);
    editor->setIndentationWidth(0);
    editor->setTabWidth(4);
    editor->setTabIndents(true);

    //setup margins
    editor->setMarginLineNumbers(1, true);
    editor->setMarginWidth(1, QString("1"));

    //setup brace matching
    editor->setBraceMatching(QsciScintilla::SloppyBraceMatch);
    editor->setMatchedBraceBackgroundColor(Qt::gray);
    editor->setUnmatchedBraceForegroundColor(Qt::black);
}

void CPlusPLus::setupDefaultLexer(QsciScintilla* editor)
{
    QsciLexerCPP* lexer = new QsciLexerCPP(editor);
    lexer->setFoldComments(true);
    lexer->setFoldAtElse(true);
    lexer->setFoldPreprocessor(true);

    #if defined Q_WS_WIN
        int font_id = QFontDatabase::addApplicationFont(":/fonts/COURI.TTF");
        lexer->setFont(QFont(QFontDatabase::applicationFontFamilies(font_id).first()));
    #elif defined Q_WS_X11
        lexer->setFont(QFont("Monospace"));
    #endif
    // setup colors
    lexer->setColor(QColor(0x00, 0x80, 0x00), QsciLexerCPP::Comment);
    lexer->setColor(QColor(0x00, 0x80, 0x00), QsciLexerCPP::CommentLine);
    lexer->setColor(Qt::darkBlue, QsciLexerCPP::Number);
    lexer->setColor(QColor(0x00, 0x00, 0xff), QsciLexerCPP::Keyword);
    lexer->setColor(QColor(0xa3, 0x15, 0x15), QsciLexerCPP::DoubleQuotedString);
    lexer->setColor(QColor(0xa3, 0x15, 0x15), QsciLexerCPP::SingleQuotedString);
    lexer->setColor(QColor(0x00, 0x00, 0xff), QsciLexerCPP::PreProcessor);

    lexer->setAutoIndentStyle(QsciScintilla::AiOpening);

    editor->setLexer(lexer);
}

QWidget* CPlusPLus::settings()
{
    return 0;
}

true CPlusPLus::openFile(QTabWidget* parent, QString path)
{
    if(parent != 0 && !path.isEmpty())
    {
        QsciScintilla *sourceEditor = new QsciScintilla(parent);
        QFile contentManager (path);
        if(contentManager.open(QFile::ReadOnly))
        {
            sourceEditor->setText(contentManager.readAll());
            configureEditor(sourceEditor);
            QFileInfo info(path);
            parent->addTab(sourceEditor,info.fileName());
            parent->setCurrentWidget(sourceEditor);
        }
        else
        {
            QMessageBox::critical(0, "Can't open file",contentManager.errorString());
            return false;
        }
    }
    else
    {
        QMessageBox::critical(0,"Cant open file", "Wrong path or program bug. Please send us log file to remove bugs like this. This operation is private");
        return false;
    }
    QMessageBox::critical(0, "Sorry", "Sorry, but it's a bug. Please send us log file. We'll try to remove this bug. Follow Help->Send log file");
    return false;
}

bool CPlusPLus::compile(QStringList files, QString env)
{
    QStringList args;
    args<<"-o"<<files;
    m_cmpPrc->start("gcc", args);
}

Q_EXPORT_PLUGIN2(cplusplus, CPlusPLus)
